package com.tyj.tuyijiesystem.model.juser.service.impl;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.tyj.tuyijiecommon.util.JwtUtil;
import com.tyj.tuyijiesystem.model.juser.dao.JUserDao;
import com.tyj.tuyijiesystem.model.juser.entity.JUser;
import com.tyj.tuyijiesystem.model.juser.service.JUserService;
import com.tyj.tuyijiecommon.exception.UpdateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/6/8
 */
@Service
public class JUserServiceImpl implements JUserService {
    private static Logger log = LoggerFactory.getLogger(JUserServiceImpl.class);
    @Resource
    private JUserDao jUserDao;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private JwtUtil jwtUtil;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return jUserDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(JUser record) {
        return jUserDao.insert(record);
    }

    @Override
    public boolean insertSelective(JUser record, String code, String password) {
        Assert.notNull(code, "验证码不能为空");
        Assert.notNull(password, "密码不能为空");
        String code1 = redisTemplate.opsForValue().get("register:code:" + record.getPhone()).toString();

        //验证码错误
        if (!StrUtil.equals(code, code1)) {
            return false;
        }
//        密码加密
        record.setPassword(passwordEncoder.encode(password));
        if (jUserDao.insertSelective(record) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public JUser selectByPrimaryKey(Long id) {
        return jUserDao.selectByPrimaryKey(id);
    }

    @Override
    public JUser updateByPrimaryKeySelective(JUser record) throws UpdateException {
        if (StrUtil.isNotEmpty(record.getPassword())) {
            record.setPassword(passwordEncoder.encode(record.getPassword()));
        }
        if (jUserDao.updateByPrimaryKeySelective(record) <= 0) {
            throw new UpdateException("更新失败");
        }
        return this.selectByPrimaryKey(record.getId());
    }

    @Override
    public int updateByPrimaryKey(JUser record) {
        return jUserDao.updateByPrimaryKey(record);
    }

    @Override
    public boolean existJUser(String phone) {
        if (this.jUserDao.existJUser(phone) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Integer updatePassword(String newVal, String oldVal, Long uid) {

        String val1 = passwordEncoder.encode(newVal);
        String val2 = passwordEncoder.encode(oldVal);
        String credentials = (String) SecurityContextHolder.getContext().getAuthentication().getCredentials();
        if (!val2.equals(credentials)) {
//            密码不正确
            return -1;
        }

        JUser juser = new JUser();
        juser.setId(uid);
        juser.setPassword(val1);
        if (this.jUserDao.updateByPrimaryKeySelective(juser) > 0) {
//            修改成功
            return 1;
        }
//       修改失败
        return 0;
    }
}
